﻿using System;
using System.Windows.Forms;

namespace Browser
{
    public partial class Login : Form
    {
        private readonly Func<string, string, string> _checkUserFunc;
        public string UserID { get; set; }

        public Login(Func<string, string, string> checkUserFunc)
        {
            InitializeComponent();
            _checkUserFunc = checkUserFunc;
        }

        private bool Check()
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                errorProvider1.SetError(textBox1,"请输入登录名称");
                return false;
            }
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                errorProvider1.SetError(textBox1, "请输入登录密码");
                return false;
            }
            return true;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!Check()) return;
            try
            {
                UserID = _checkUserFunc(textBox1.Text, MD5.GetMD5(textBox2.Text));
                if (!string.IsNullOrEmpty(UserID))
                {
                    DialogResult = DialogResult.OK;
                    if (checkBox1.Checked)
                    {
                        SaveUser();
                    }
                    else
                    {
                        ForgetUser();
                    }
                    Close();
                }
                else
                {
                    errorProvider1.SetError(textBox1, "用户登录失败");
                }
            }
            catch (System.Net.WebException exception)
            {
                LogHelper.WriteLog(exception);
                MyMessageBox.ShowErrorMsg("网络连接失败，请联系系统管理员");
            }
            catch (Exception exception)
            {
                LogHelper.WriteLog(exception);
                MyMessageBox.ShowErrorMsg("登录过程中发生异常，请联系系统管理员");
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            try
            {
                checkBox1.Checked = ParametersManager.IsRememberUser;
                if (checkBox1.Checked)
                {
                    textBox1.Text = ParametersManager.UserName;
                    textBox2.Text = ParametersManager.Password;
                } 
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
                checkBox1.Checked = false;
                checkBox1.Enabled = false;
                MyMessageBox.ShowWarningMsg("之前记住的用户信息读取失败，但程序可以正常运行，请通知系统管理员");
            }
        }

        private void SaveUser()
        {
            try
            {
                ParametersManager.RememberUser(textBox1.Text, textBox2.Text);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
            }
        }
        private void ForgetUser()
        {
            try
            {
                ParametersManager.ForgetUser();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
            }
        }
    }
}
